package com.highspeed.bdk.controller.base;

import com.bianmaba.spring.data.jpa.domain.Result;
import com.bianmaba.spring.mvc.method.support.annotations.JSON;
import com.highspeed.bdk.service.base.IBdkBaseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.Serializable;
import java.util.List;

public abstract class BdkCrudController<T, ID extends Serializable> extends BdkReadController<T, ID> {

    protected IBdkBaseService<T, ID> getService() {
        return null;
    }

    @JSON()
    @ApiOperation(value = "保存单和数据", notes = "返回数据中的success为true表示删除成功")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody @ApiParam(name = "entity") T entity) {
        T data = getService().save(entity);
        Result result = new Result(true, "数据保存成功！", data);
        return result;
    }

    @JSON()
    @ApiOperation(value = "保存数据集合", notes = "返回数据中的success为true表示删除成功")
    @RequestMapping(value = "/saves", method = RequestMethod.POST)
    public Result save(@RequestBody @ApiParam(name = "entity") List<T> entities) {
        List<T> data = getService().save(entities);
        Result result = new Result(true, "数据保存成功！", data);
        return result;
    }


    @JSON()
    @ApiOperation(value = "删除指定ID列表的所有数据", notes = "返回数据中的data为true表示删除成功")
    @RequestMapping(value = "/deletes", method = RequestMethod.POST)
    public Result deletes(ID[] ids) {
        getService().delete(ids);
        Result result = new Result(true, "数据删除成功！", true);
        return result;
    }

    @JSON()
    @ApiOperation(value = "删除指定ID的数据", notes = "返回数据中的data为true表示删除成功")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(ID id) {
        getService().delete(id);
        Result result = new Result(true, "数据删除成功！", true);
        return result;
    }
}
